package javass;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;


public class Fangfa {
	/**
	 * 插入
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int ishave(String userid) throws SQLException, ClassNotFoundException{
		int a=3;
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn=connutil.getConn();
		PreparedStatementUtil psu=new PreparedStatementUtil();
		ResultSetUtil rsu=new ResultSetUtil();
		String sql=" SELECT u.id FROM userinfo u WHERE u.username=?;";
		PreparedStatement ps=psu.getPreparedStatement(conn, sql);
		ps.setString(1,userid);
		ResultSet rs=rsu.getResultSet(ps);
		if(rs.next()){
			a=1;
		}else{
			a=0;
		}
		conn.close();
		ps.close();
		return a;
	}
	public void inser(Userinfo ui ) throws SQLException, ClassNotFoundException{
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn=connutil.getConn();
		PreparedStatementUtil psu=new PreparedStatementUtil();
		String sql=" INSERT INTO userinfo(username,`password`,email)VALUES(?,?,?); ";
		PreparedStatement ps=psu.getPreparedStatement(conn, sql);
		ps.setString(1,ui.getUsername());
		ps.setString(2,ui.getPassword() );
		ps.setString(3, ui.getEmail());
		ps.executeUpdate();
		conn.close();
		ps.close();
	}
	public String login(Userinfo ui) throws ClassNotFoundException, SQLException{
		String str="0";
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn=connutil.getConn();
		PreparedStatementUtil psu=new PreparedStatementUtil();
		ResultSetUtil rsu=new ResultSetUtil();
		String sql=" SELECT u.`password`FROM userinfo u WHERE u.username=?;";
		PreparedStatement ps=psu.getPreparedStatement(conn, sql);
		ps.setString(1,ui.getUsername());
		ResultSet rs=rsu.getResultSet(ps);
		if(rs.next()){
			if (rs.getString(1).equals(ui.getPassword())) {
				str="1";
			}else{
				str="0";
			}
		}else{
			str="0";
		}
		conn.close();
		ps.close();
		return str;
	}
	public void insertTie(Fatie ft) throws ClassNotFoundException, SQLException{
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn=connutil.getConn();
		PreparedStatementUtil psu=new PreparedStatementUtil();
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String a=df.format(new Date());// new Date()为获取当前系统时间
		String sql="INSERT into tie(title,userid,tieinfo,date)VALUES(?,(SELECT u.id FROM userinfo u WHERE u.username=? LIMIT 0,1),?,?);";
		PreparedStatement ps=psu.getPreparedStatement(conn, sql);
		ps.setString(1,ft.getTitle());
		ps.setString(2,LoginServlet.n1);
		ps.setString(3,ft.getTieinfo());
		ps.setString(4,a);
		ps.executeUpdate();
		conn.close();
		ps.close();
	}
	public int update1(String a,String c) throws ClassNotFoundException, SQLException{
		int n=0;
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn=connutil.getConn();
		PreparedStatementUtil psu=new PreparedStatementUtil();
		Userinfo u=new Userinfo();
		u.setUsername(Userinfo.username);
		u.setPassword(c);
		System.out.println(u.getPassword()+u.getUsername());
		String m="0";
		m=this.login(u);
		System.out.println("m="+m);
		if(m.equals("1")){
			String sql="UPDATE userinfo u SET u.`password`=? WHERE u.username=? AND u.`password`=?;";
			PreparedStatement ps=psu.getPreparedStatement(conn, sql);
			ps.setString(1,a);
			ps.setString(2,Userinfo.username);
			ps.setString(3,c);
			ps.executeUpdate();
			conn.close();
			ps.close();
			n=1;
		}else{
			n=0;
		}
		return n;
	}

}
